package com.seari.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seari.bean.FaultInfo;
import com.seari.bean.FaultNum;
import com.seari.bean.FlowMajor;
import com.seari.bean.FlowText;
import com.seari.bean.PictureInfo;
import com.seari.bean.User;
import com.seari.pojo.MessageBean;
import com.seari.service.EquipmentTreeService;
import com.seari.service.FaultService;
import com.seari.service.LocationService;
import com.seari.utils.DateUtil;
import com.seari.utils.HandlerNullUtils;

/**
 * 故障
 * 
 * @author liuqi
 *
 */

@Controller
public class FaultController {

	@Autowired
	private FaultService faultService;
	
	@Autowired
	private LocationService locationService;
	
	@Autowired
	private EquipmentTreeService equipmentTreeService;

	/**
	 * 应急事件统计 获取应急事件 处理完成数 发生数
	 * @return
	 */
	@RequestMapping(value = "/fault/getFaultNum")
	@ResponseBody
	public MessageBean getFaultNum() {

		MessageBean messageBean = new MessageBean();
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();

			FaultNum faultNum = faultService.getFaultNum();
			dataMap.put("faultNum", faultNum);

			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("应急事件数量获取成功 ");
			messageBean.setData(dataMap);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("应急事件数量获取失败");
		}

		return messageBean;
	}
	
	/**
	 * 故障图片
	 * @return
	 */
	@RequestMapping(value = "/fault/getFaultPictureListInfo")
	@ResponseBody
	public MessageBean getFaultPictureListInfo(@RequestParam Map<String, Object> map) {

		MessageBean messageBean = new MessageBean();
		try {
			Map<String, Object> dataMap = new HashMap<String, Object>();
			String faultId = (String) map.get("faultId");
			
			List<PictureInfo> pictureInfoList = faultService.getFaultPictureListInfo(faultId);
			dataMap.put("pictureInfoList", pictureInfoList);

			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("故障图片获取成功 ");
			messageBean.setData(dataMap);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("故障图片获取失败");
		}

		return messageBean;
	}

	/**
	 * 获取故障详情信息
	 *  条件 faultId
	 *  type为 1 查询当天   type为 2 查询历史
	 *  dataType 为 1 查询   本地mysql故障详情数据      为2 查询小电科故障详情数据
	 * @return
	 */
	@RequestMapping(value = "/fault/getFaultInfoById")
	@ResponseBody
	public MessageBean getFaultInfoById(@RequestBody Map<String, Object> map) {

		MessageBean messageBean = new MessageBean();
		try {

			String faultId = (String) map.get("faultId");
			String type = (String) map.get("type");
			Map<String, Object> dataMap = new HashMap<String, Object>();
			FaultInfo faultInfo = new FaultInfo();
			//type为1查询当天的故障数据      其它类型查询的是历史的故障数据(聚群)
			if ("1".equals(type)) {
				String dataType = (String) map.get("dataType");
				//dataType 为1代表由小电科同步过去的数据
				if("1".equals(dataType)) {
					//查询小电科的视图
					faultInfo = faultService.getRtFaultInfoById(faultId);
					//dataType 为其他代表  流程系统同步过去的数据
				}else {
					//查询本地的表
					faultInfo = faultService.getRtFaultInfoByIdFromFlow(faultId);

				}
			} else {
				faultInfo = faultService.getHisFaultInfoById(faultId);
			}
			dataMap.put("faultInfo", faultInfo);

			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("故障详情信息获取成功 ");
			messageBean.setData(dataMap);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("故障详情信息获取失败");
		}

		return messageBean;
	}
	
	
	
	/**
	 * 获取处理完成故障列表信息 分页 5 条
	 * @return
	 */
	@RequestMapping(value = "/fault/getResolveFaultListInfo")
	@ResponseBody
	public MessageBean getResolveFaultListInfo(@RequestBody Map<String, Object> map) {

		Integer pageNum =  (Integer) map.get("page");
		Integer pageSize =  (Integer) map.get("limit");
		
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 5 : pageSize;
		int pageNumInt = pageNum;
		int pageSizeInt = pageSize;
		
		MessageBean messageBean = new MessageBean();
		try {
			List<FaultInfo> faultInfoList = new ArrayList<FaultInfo>();
			
			//type为1查询当天的故障数据        其它类型查询的是历史的故障数据
//			if("1".equals(type)) {
				PageHelper.startPage(pageNumInt, pageSizeInt);
				faultInfoList = faultService.getResolveRtFaultListInfo();
//			}else {
//				Map<String, Object> mapIn = new HashMap<String, Object>();
//				if(!HandlerNullUtils.objectIsNull(map.get("faultLine"))) {
//					String faultLine = (String) map.get("faultLine");
//					//故障线路
//					mapIn.put("faultLine", faultLine);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzZy"))) {
//					String gzZy = (String) map.get("gzZy");
//					//故障专业
//					mapIn.put("gzZy", gzZy);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb"))) {
//					String gzSb = (String) map.get("gzSb");
//					//故障设备  故障树
//					mapIn.put("gzSb", gzSb);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb1"))) {
//					String gzSb1 = (String) map.get("gzSb1");
//					mapIn.put("gzSb1", gzSb1);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb2"))) {
//					String gzSb2 = (String) map.get("gzSb2");
//					mapIn.put("gzSb2", gzSb2);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("match"))) {
//					String queryInput = (String) map.get("match");
//					//查询输入条件
//					mapIn.put("queryInput", queryInput);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("date"))) {
//					String date = ((String) map.get("date")).replace("—","");
//					mapIn.put("dateTime", date);
//				}
//				//故障线路
//				PageHelper.startPage(pageNumInt, pageSizeInt);
//	            faultInfoList = faultService.getResolveHisFaultListInfo(mapIn);
//			}
			PageInfo<FaultInfo> page = new PageInfo<FaultInfo>(faultInfoList);
			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setCount(page.getTotal());
			messageBean.setMessage("处理完成故障列表信获取成功 ");
			messageBean.setData(page.getList());
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("处理完成故障列表信获取失败");
		}

		return messageBean;
	}

	/**
	 * 获取未处理完成故障列表信息 分页 10 条
	 * @return
	 */
	@RequestMapping(value = "/fault/getUnResolveFaultListInfo")
	@ResponseBody
	public MessageBean getUnResolveFaultListInfo(@RequestBody Map<String, Object> map) {

		Integer pageNum =  (Integer) map.get("page");
		Integer pageSize =  (Integer) map.get("limit");
		//String type = (Integer) map.get("type")+"";
		
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10 : pageSize;
		int pageNumInt = pageNum;
		int pageSizeInt = pageSize;

		MessageBean messageBean = new MessageBean();
		try {
			List<FaultInfo> faultInfoList = new ArrayList<FaultInfo>();
			
			//type为 1 查询当天的故障数据    其它类型查询的是历史的故障数据(聚群)
			//if("1".equals(type)) {
				PageHelper.startPage(pageNumInt, pageSizeInt);
				faultInfoList = faultService.getUnResolveRtFaultListInfo();
			//}else {
				
//				Map<String, Object> mapIn = new HashMap<String, Object>();
//				if(!HandlerNullUtils.objectIsNull(map.get("faultLine"))) {
//					String faultLine = (String) map.get("faultLine");
//					//故障线路
//					mapIn.put("faultLine", faultLine);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzZy"))) {
//					String gzZy = (String) map.get("gzZy");
//					//故障专业
//					mapIn.put("gzZy", gzZy);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb"))) {
//					String gzSb = (String) map.get("gzSb");
//					//故障设备  故障树
//					mapIn.put("gzSb", gzSb);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb1"))) {
//					String gzSb1 = (String) map.get("gzSb1");
//					mapIn.put("gzSb1", gzSb1);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb2"))) {
//					String gzSb2 = (String) map.get("gzSb2");
//					mapIn.put("gzSb2", gzSb2);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("match"))) {
//					String queryInput = (String) map.get("match");
//					//查询输入条件
//					mapIn.put("queryInput", queryInput);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("date"))) {
//					String date = ((String) map.get("date")).replace("—","");
//					mapIn.put("dateTime", date);
//				}
//	
//				PageHelper.startPage(pageNumInt, pageSizeInt);
//	            faultInfoList = faultService.getUnResolveHisFaultListInfo(mapIn);
//			}
			
			PageInfo<FaultInfo> page = new PageInfo<FaultInfo>(faultInfoList);
			if(page.getList().size()!=0) {
				locationService.fillLocationOfFaultInfo(page.getList());
			}
			messageBean.setData(page.getList());
			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setCount(page.getTotal());
			messageBean.setMessage("获取未处理完成故障列表信获取成功 ");
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("获取未处理完成故障列表信息获取失败");
		}

		return messageBean;
	}
		
	/**
	 * 查询故障历史信息
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/fault/getQueryhistoryFaultListInfo")
	@ResponseBody
	public MessageBean getQueryhistoryFaultListInfo(@RequestBody Map<String, Object> map) {
		
		Integer pageNum =  (Integer) map.get("page");
		Integer pageSize =  (Integer) map.get("limit");
		
		pageNum = pageNum == null ? 1 : pageNum;
		pageSize = pageSize == null ? 10 : pageSize;
		int pageNumInt = pageNum;
		int pageSizeInt = pageSize;

		MessageBean messageBean = new MessageBean();
		try {
			List<FaultInfo> faultInfoList = new ArrayList<FaultInfo>();
			
				Map<String, Object> mapIn = new HashMap<String, Object>();
				if(!HandlerNullUtils.objectIsNull(map.get("faultLine"))) {
					String faultLine = (String) map.get("faultLine");
					String faultLineStr = Integer.parseInt(faultLine)+"";
					//故障线路
					mapIn.put("faultLine", faultLineStr);
				}
				if(!HandlerNullUtils.objectIsNull(map.get("gzZy"))) {
					String gzZy = (String) map.get("gzZy");
					//故障专业
					mapIn.put("gzZy", gzZy);
				}
				if(!HandlerNullUtils.objectIsNull(map.get("gzSb"))) {
					String gzSb = (String) map.get("gzSb");
					//故障设备  故障树
					Map<String, List<String>> paramMap = equipmentTreeService.getMenuParams(gzSb);
					if(!HandlerNullUtils.objectIsNull(paramMap.get("lv1"))) {
						List<String> gzSb1 = paramMap.get("lv1");
						mapIn.put("gzSb1", gzSb1);
					}
					if(!HandlerNullUtils.objectIsNull(paramMap.get("lv2"))) {
						List<String> gzSb2 = paramMap.get("lv2");
						mapIn.put("gzSb2", gzSb2);
					}
					if(!HandlerNullUtils.objectIsNull(paramMap.get("lv3"))) {
						List<String> gzSb3 = paramMap.get("lv3");
						mapIn.put("gzSb3", gzSb3);
					}
				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb1"))) {
//					String gzSb1 = (String) map.get("gzSb1");
//					mapIn.put("gzSb1", gzSb1);
//				}
//				if(!HandlerNullUtils.objectIsNull(map.get("gzSb2"))) {
//					String gzSb2 = (String) map.get("gzSb2");
//					mapIn.put("gzSb2", gzSb2);
//				}
				if(!HandlerNullUtils.objectIsNull(map.get("match"))) {
					String queryInput = (String) map.get("match");
					//查询输入条件
					mapIn.put("queryInput", queryInput);
				}
				if(!HandlerNullUtils.objectIsNull(map.get("date"))) {
				//	String date = ((String) map.get("date")).replaceAll("—", "");
					String date = (String) map.get("date");

					mapIn.put("dateTime", date);
				}
	
				PageHelper.startPage(pageNumInt, pageSizeInt);
	            faultInfoList = faultService.getQueryhistoryFaultListInfo(mapIn);
			
				PageInfo<FaultInfo> page = new PageInfo<FaultInfo>(faultInfoList);
				messageBean.setType("success");
				messageBean.setCode("1000");
				messageBean.setCount(page.getTotal());
				messageBean.setMessage("获取未处理完成故障列表信获取成功 ");
				messageBean.setData(page.getList());
			} catch (Exception e) {
				e.printStackTrace();
				messageBean.setType("fail");
				messageBean.setCode("3000");
				messageBean.setMessage("获取未处理完成故障列表信息获取失败");
			}
		return messageBean;
	}
	
	/**
	 * 获取处理故障用户信息
	 * @return
	 */
	@RequestMapping("/fault/getHanderFaultUserInfo")
	@ResponseBody
	public MessageBean getHanderFaultUserInfo(@RequestBody Map<String,Object> map) {
		
		MessageBean messageBean = new MessageBean();
		try {
			String faultId = (String)map.get("faultId");
			
			List<User> users = faultService.getHanderFaultUserInfo(faultId);
			
			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("获取处理故障用户信息获取成功");
			messageBean.setData(users);
		}catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("获取处理故障用户信息获取失败");
		}
		return messageBean;
		
	}

	/**
	 * 专业流程
	 * @param
	 * @return
	 */
	@RequestMapping("/fault/getFlowMajorStatu")
	@ResponseBody
	public MessageBean getFlowMajorStatu(@RequestParam Map<String,Object> map) {

		MessageBean messageBean = new MessageBean();
		try {
			String faultId = (String)map.get("faultId");
			String tag = (String)map.get("tag");

			Map<String, Object> dataMap = new HashMap<String, Object>();
			List<FlowMajor> flowMajorList = faultService.getFlowMajorStatu(faultId,tag);
			for (FlowMajor flowMajor : flowMajorList) {
				String firstTime = flowMajor.getDisposeTime();
				String secTime = flowMajor.getAnswerTime();
				if (firstTime == null||firstTime.equals("")||secTime==null||secTime.equals("")) {
					flowMajor.setSubTime("--");
				}else{
					long subTime = DateUtil.timeSub(secTime,firstTime)/60;
					flowMajor.setSubTime(String.valueOf(subTime));
				}
			}
			dataMap.put("faultMainFlow", flowMajorList);

			messageBean.setCount(flowMajorList.size());
			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("故障处理流程信息获取成功 ");
			messageBean.setData(dataMap);
		}catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("故障处理流程信息获取失败");
		}
		return messageBean;
	}
	
	/**
	 * 总流程
	 * @return
	 */
	@RequestMapping(value = "/fault/getFlowMajorAll")
	@ResponseBody
	public MessageBean getFlowMajorAll(String faultId){
		
		MessageBean messageBean = new MessageBean();
		try {

			Map<String, Object> dataMap = new HashMap<String, Object>();

			List<FlowMajor> flowMajorAllList = faultService.getFlowMajorAll(faultId);
			List<Integer> majorList = faultService.getFlowAllMajor(faultId);

			for (FlowMajor flowMajor : flowMajorAllList) {
				String firstTime = flowMajor.getDisposeTime();
				String secTime = flowMajor.getAnswerTime();
				if (firstTime == null||firstTime.equals("")||secTime==null||secTime.equals("")) {
					flowMajor.setSubTime("--");
				}else{
					long subTime = DateUtil.timeSub(secTime,firstTime)/60;
					flowMajor.setSubTime(String.valueOf(subTime));
				}
			}

			dataMap.put("majorList", majorList);
			dataMap.put("diffList", getDisAnsTimeDiff(faultId));
		    dataMap.put("flowMajorAllList", flowMajorAllList);


			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setCount(flowMajorAllList.size());
			messageBean.setMessage("故障处理流程信息获取成功 ");
			messageBean.setData(dataMap);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("故障处理流程信息获取失败");
		}

		return messageBean;
	}
	
	public List<Map<String,Object>> getDisAnsTimeDiff(String faultId) {
	    Map<String,Object> diffMap = new HashMap<String,Object>();
	    Integer ansDiff = faultService.getAnswerTimeDiff(faultId);
	    
	    String ansString = "";
	    if(ansDiff!=null) {
	    	ansString = DateUtil.minToHoursToDays(ansDiff);
	    }
	    diffMap.put("ansDiff", ansString);
	    
	    Integer disDiff = faultService.getDisposeTimeDiff(faultId);
	    String disString = "";
	    if(disDiff!=null) {
	    	 disString = DateUtil.minToHoursToDays(disDiff);
	    }
	    diffMap.put("disDiff", disString);
	    
		FaultInfo faultInfo = faultService.getRtFaultInfoByIdFromFlow(faultId);
	    if(!HandlerNullUtils.objectIsNull(faultInfo)) {
			diffMap.put("faultType",faultInfo.getFaultType());
	    }

		List list = new ArrayList();
	    list.add(diffMap);
	    return list;
	}
	
	/**
	 *
	 * 故障抢修方案展示
	 */
	@RequestMapping(value = "/fault/getFaultPlanShow")
	@ResponseBody
	public MessageBean getFaultPlanShow (@RequestBody Map<String, Object> map){

		MessageBean messageBean = new MessageBean();
		try {
			Map<String, String> mapIn = new HashMap<String, String>();
			if(!HandlerNullUtils.objectIsNull(map.get("faultId"))) {
				String faultId = (String) map.get("faultId");
				mapIn.put("faultId", faultId);
			}
			if(!HandlerNullUtils.objectIsNull(map.get("tag"))) {
				String tag = (String) map.get("tag");
				mapIn.put("tag", tag);
			}
			List<FlowText> flowTextList = faultService.getFaultPlanShow(mapIn);
			messageBean.setType("success");
			messageBean.setCode("1000");
			messageBean.setMessage("故障抢修方案展示获取成功 ");
			messageBean.setData(flowTextList);
		} catch (Exception e) {
			e.printStackTrace();
			messageBean.setType("fail");
			messageBean.setCode("3000");
			messageBean.setMessage("故障抢修方案展示获取失败");
		}

		return messageBean;

	}
}
